//
//  SYShowNewsController.m
//  MicroNews
//
//  Created by tongcheng on 15/10/20.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYShowNewsController.h"
#import "NewsCell.h"
#import "NewsModel.h"

@interface SYShowNewsController()

@property (strong,nonatomic)NSMutableArray *newsArray;

@end

@implementation SYShowNewsController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
//    [self.tableView addHeaderWithTarget:self action:@selector(loadData)];
//    [self.tableView addFooterWithTarget:self action:@selector(loaMoreData)];
    
    //上啦下拉刷新
    __block SYShowNewsController *blockSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [blockSelf loadData];
        
    }];
    
    [self.tableView headerBeginRefreshing];


}

- (void)loadData
{
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        //停止刷新
//        [self.tableView headerEndRefreshing];
//    });
    

    NSString *url =  SYNSString(@"%@%@",WDURL,WDIndex);
    [[NetworkInterface shareInstance] requestForGet:url complete:^(NSDictionary *result){
        NSLog(@"-->SYAddressViewController--------》%@",result);
        
        //NSMutableArray *userArray = [SYAddressModel objectArrayWithKeyValuesArray:result[@"list"]];
        self.newsArray =  [NewsModel didNews:result[@"data"]];
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        
    } error:^(NSError *error){
    }];
}
- (void)loaMoreData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //停止刷新
        [self.tableView footerEndRefreshing];
    });
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.newsArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    //    声明静态字符串型对象，用来标记重用单元格
//    static NSString *TableSampleIdentifier = @"NewsCell";
//    //    用TableSampleIdentifier表示需要重用的单元
//    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    
    // 1. 创建cell
    NewsCell *cell = [NewsCell cellWithTable:tableView];
    
    //通过数据模型 设置Cell内容
    cell.news = self.newsArray[indexPath.row];

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.Array removeObjectAtIndex:indexPath.row];
//    
//    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

@end
